package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.enumeration.GoodsOrderByEnum;
import haizhi.fruitmall.enumeration.PictureEnum;
import haizhi.fruitmall.pojo.form.GoodsCategoryForm;
import haizhi.fruitmall.pojo.form.GoodsForm;
import haizhi.fruitmall.pojo.form.PictureForm;
import haizhi.fruitmall.pojo.po.Picture;
import haizhi.fruitmall.pojo.view.GoodsUnitView;
import haizhi.fruitmall.pojo.view.PictureView;
import haizhi.fruitmall.service.GoodsCategoryService;
import haizhi.fruitmall.service.GoodsUnitService;
import haizhi.fruitmall.service.IndexService;
import haizhi.fruitmall.service.PictureService;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by richa on 2017/9/13
 */
@Service
@Transactional
public class IndexServiceImpl implements IndexService {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private GoodsUnitService goodsUnitService;

    @Autowired
    private PictureService pictureService;

    @Override
    public Map getHead(){
        Map<String, Object> dataMap = new LinkedHashMap<>();
        //图片，滑动图片，静态图片
        List<Picture> pictureList = pictureService.getall(new PictureForm());
        List<PictureView> slidedPictureViewList = new ArrayList<>();
        List<PictureView> staticPictureViewList = new ArrayList<>();
        for(Picture picture : pictureList){
            PictureView pictureView = new PictureView();
            BeanUtil.copyBean(picture,pictureView);
            if(PictureEnum.SLIDED_PICTURE.getCode().equals(picture.getPictureType())){
                slidedPictureViewList.add(pictureView);
            }else{
                staticPictureViewList.add(pictureView);
            }
        }
        Map<String,Object> pictureMap = new LinkedHashMap<>();
        pictureMap.put("slidedPicture",slidedPictureViewList);
        pictureMap.put("staticPicture",staticPictureViewList);
        dataMap.put("picture",pictureMap);
        //商品分类
        dataMap.put("goodsCategory", goodsCategoryService.getView(new GoodsCategoryForm(true)));
        return dataMap;
    }

    @Override
    public Map getTail() {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        ////商品单元
        Map<String,Object> goodsUnitMap = new LinkedHashMap<>();
        //精品
        GoodsForm goodsForm = new GoodsForm();
        goodsForm.setPageNum(1);
        goodsForm.setPageSize(10);
        goodsForm.setIsSpecial(true);
        List<GoodsUnitView> specialUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("specialUnit",specialUnitViewList);
        goodsForm.setIsSpecial(null);
        //热销
        goodsForm.setOrderBy(GoodsOrderByEnum.TOTAL_SALES.getCode());
        List<GoodsUnitView> popularUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("popularUnit",popularUnitViewList);
        goodsForm.setOrderBy(null);
        //分类
        //手机 336999576
        goodsForm.setCategoryId(336999576);
        List<GoodsUnitView> phoneUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("phoneUnit",phoneUnitViewList);
        //台式机 542872254
        goodsForm.setCategoryId(542872254);
        List<GoodsUnitView> desktopUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("desktopUnit", desktopUnitViewList);
        //笔记本 963491318
        goodsForm.setCategoryId(963491318);
        List<GoodsUnitView> laptopUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("laptopUnit", laptopUnitViewList);
        //平板 439821457
        goodsForm.setCategoryId(439821457);
        List<GoodsUnitView> padUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("padUnit", padUnitViewList);
        //显示器 424533153
        goodsForm.setCategoryId(424533153);
        List<GoodsUnitView> displayerUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("displayerUnit", displayerUnitViewList);
        //打印机 423915883
        goodsForm.setCategoryId(423915883);
        List<GoodsUnitView> printerUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("printerUnit", printerUnitViewList);
        //肆拾玖坊 847539846
        goodsForm.setCategoryId(847539846);
        List<GoodsUnitView> wineUnitViewList = goodsUnitService.getView(goodsForm);
        goodsUnitMap.put("wineUnit", wineUnitViewList);
        //显示商品单元
        dataMap.put("goodsUnit",goodsUnitMap);

        return dataMap;
    }

}